package be.redo.model.entities;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Future;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.format.annotation.DateTimeFormat;

/**
 * @author Frank Plain old Java class to represent a database table row.
 *         Represents a the number of nights of a stay in the domain model.
 * 
 */
@Entity
@NamedQueries({
		@NamedQuery(name = "findAantalStays", query = "select count(s) from Stay s"),
		@NamedQuery(name = "findAllStays", query = "select s from Stay s"),
		@NamedQuery(name = "findStaysByResidence", query = "select s from Stay s where s.residence = :residence"),
		@NamedQuery(name = "findStaysByVacation", query = "select s from Stay s where s.vacation = :vacation") })
public class Stay implements Serializable {

	/**
	 * Generated serialVersionUID.
	 */
	private static final long serialVersionUID = 54538130652080423L;

	/**
	 * Number of nights the customer stays in the residence. Must be at least 1.
	 */
	@NotNull
	@Min(1)
	private int numberOfNights;

	/**
	 * Day of the first night stay in the residence. Must be in the future whe
	 * the stay is booked.
	 */
	@Future
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "M-")
	private Date checkinDate;

	/**
	 * vacation containing this Stay.
	 */
	@NotNull
	@ManyToOne
	@JoinColumn(name = "vacation")
	private Vacation vacation;

	/**
	 * Residence in witch the customer stays.
	 */
	@NotNull
	@ManyToOne
	@JoinColumn(name = "residence")
	private Residence residence;

	/**
	 * Generated key by the Database
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "stay_id")
	private Long StayId;

	public Long getStayId() {
		return this.StayId;
	}

	public void setStayId(Long id) {
		this.StayId = id;
	}

	public int getNumberOfNights() {
		return this.numberOfNights;
	}

	public void setNumberOfNights(int numberOfNights) {
		this.numberOfNights = numberOfNights;
	}

	public Date getCheckinDate() {
		return this.checkinDate;
	}

	public void setCheckinDate(Date checkinDate) {
		this.checkinDate = checkinDate;
	}

	public Vacation getVacation() {
		return this.vacation;
	}

	public void setVacation(Vacation vacation) {
		this.vacation = vacation;
	}

	public Residence getResidence() {
		return this.residence;
	}

	public void setResidence(Residence residence) {
		this.residence = residence;
	}

	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}

	/**
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((checkinDate == null) ? 0 : checkinDate.hashCode());
		result = prime * result + numberOfNights;
		result = prime * result
				+ ((residence == null) ? 0 : residence.hashCode());
		result = prime * result
				+ ((vacation == null) ? 0 : vacation.hashCode());
		return result;
	}

	/**
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Stay other = (Stay) obj;
		if (checkinDate == null) {
			if (other.checkinDate != null) {
				return false;
			}
		} else if (!checkinDate.equals(other.checkinDate)) {
			return false;
		}
		if (numberOfNights != other.numberOfNights) {
			return false;
		}
		if (residence == null) {
			if (other.residence != null) {
				return false;
			}
		} else if (!residence.equals(other.residence)) {
			return false;
		}
		if (vacation == null) {
			if (other.vacation != null) {
				return false;
			}
		} else if (!vacation.equals(other.vacation)) {
			return false;
		}
		return true;
	}

}
